package offer.algorithm;


/**
 * Description：
 * Author: zhangc
 * Date：2017/7/18 14:38
 */
public class ReplaceSpace4 {

    public static String replaceSpace(String str){

        int spaceNum =0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ')
                ++ spaceNum;
        }

        char[] chars = new char[str.length() + spaceNum * 2];
        System.arraycopy(str.toCharArray(),0,chars,0,str.length());


        for (int i = str.length()-1; i >=0 ; --i) {
            if(chars[i] != ' ')
                chars[i+2*spaceNum] = chars[i];
            else {
                chars[i + 2*spaceNum - 2] = '%';
                chars[i + 2*spaceNum - 1] = '2';
                chars[i + 2*spaceNum ] = '0';
                -- spaceNum;
            }
        }
        return new String(chars);
    }

}
